# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： zabbix_alarm_format
Description :
Author : 'li'
date： 1/15/2024
-------------------------------------------------
Change Activity:
1/15/2024:
-------------------------------------------------
"""
import ll
from modules.blk.cmdb.service.network_device_asset import NETWORK_DEVICE_ASSET_SERVER
from projects.monitoring.alarm_manager.utils.format.alarm_format import AlarmFormat
from projects.monitoring.common.do.alarm import Alarm


class SpiderAlarmFormat(AlarmFormat):

    def __init__(
            self,
            subscribe_alarm_info,
            time_format="%Y-%m-%d %H:%M:%S"
    ):
        super().__init__(subscribe_alarm_info, time_format)

    def __call__(self):
        NETWORK_DEVICE_ASSET_SERVER.refresh_data()
        return super().__call__()

    def _format_alarm(self, alarm) -> Alarm:
        """
        format alarm.
        """
        if alarm['severity'] is None:
            alarm['severity'] = 'warning'
        alarm = Alarm.json_to_obj(alarm)
        alarm.ip = str(alarm.manager_ip)
        alarm.alarm_source = 'spider'
        host_name = NETWORK_DEVICE_ASSET_SERVER.get_hostname_by_ip(alarm.ip)
        if host_name is not None:
            alarm.hostname = host_name
        return alarm

    @staticmethod
    def _parse_recovery_alarm(alarm):
        """
        This function parses the recovery alarm.
        Args:
            alarm:

        Returns:

        """
        recovery_time = ll.TimeUtil.date_stamp_to_datetime(
            alarm['r_clock'],
            format_str="%Y-%m-%d %H:%M:%S"
        )
        alarm_type = 'recovery'
        recovery = True
        return recovery_time, alarm_type, recovery

    @staticmethod
    def _get_severity(severity_index):
        """

        """
        if str(severity_index) in ['0', '1', '2']:
            return 'remind'
        if str(severity_index) in ['3']:
            return 'warning'
        if str(severity_index) in ['4', '5']:
            return 'fatal'
        ll.get_logger().warning(f'Unknown severity_index: {severity_index}')
